<template>
	<!-- https://ext.dcloud.net.cn/plugin?id=404 -->

	<view class="uploadAvatar ">
		<view class="circle " @click="clk(0)">
		</view>

		<image class="uploadImg" :src="urls[0]" @click="clk(0)"></image>

		<avatar @upload="myUpload" ref="avatar"></avatar>

		<view class="uploadSuccess flex">
			<image v-if="upLoadStatus" class="gouImg" src="../../../static/Frame2.png" mode=""></image>
			<text>{{upLoadStatus?upLoadStatus:'请上传图片'}}</text>
		</view>
		<view class="sumbit-btn flex" @click="clickAvaterFn">
			点击上传
		</view>
	</view>

</template>

<script>
	import {
<<<<<<< HEAD
	  mapState,
	  mapMutations
	} from 'vuex'
=======
		mapMutations
	} from 'vuex'
	import {
		mapState
	} from 'vuex'
	import {
		mapGetters
	} from 'vuex'
	import {
		object2Url
	} from '@/utils/trans';
>>>>>>> 52b948355e151c415e9c82e94b1b31e76c5ad35e
	import avatar from "@/components/yq-avatar/yq-avatar.vue";
	export default {
		data() {
			return {
				urls: ["/static/avatar.png", "/static/avatar.png"],
				avatarObj: {},
				form:{
					head:""
				},
				
				// 上传状态
				upLoadStatus: '',
				storageMode: '',
				alyOssSignatureInfo: null,
		
				timeDate:'',
				suffix:'',
				technicianDetail:{}

			}
		},
		components: {
		
			avatar,
			
		},
		computed: {
		
			...mapGetters('user', ['taskCenter','technicianDetails']), 
			
		},
		onLoad(){
			this.getUploadConfig()
			this.init()
		},
		methods: {
<<<<<<< HEAD
			               ...mapMutations('user', ['uploadProgress']),
			myUpload(rsp) {
=======
			
			...mapMutations('user', ['uploadAvatar','uploadProgress','technicianDetailsFn']),
			init(){
				if(this.technicianDetails.authenticationNot==3){
					this.$sun.assignObject(this.form,{
						...this.technicianDetails
					})
					console.log(this.form)
				      console.log(this.form.head)
					  this.urls[0]=this.form.head
				}
				
			},
			async getUploadConfig() {
				const result = await this.$http.get({
					url: this.$api.getStorageConfig,
					data: {}
				});
				if (result.code == 200) {
					this.storageMode = result.data.storageMode; // storageMode:2是‘阿里云’上传，3是‘七牛云’ 上传
					console.log('this.storageMode', this.storageMode);

				} else {
					console.log('result.message', result.message);
				}
			},


			async myUpload(rsp) {
>>>>>>> 52b948355e151c415e9c82e94b1b31e76c5ad35e
				this.$set(this.urls, rsp.index, rsp.path);
				console.log(rsp)
				this.avatarObj = rsp
				if (this.storageMode == 3) {
					uni.uploadFile({
						url: this.$api.qnyOssSignature , //仅为示例，非真实的接口地址
						filePath: this.avatarObj.path,
						name: 'file',
						formData: {

						},
						success: (res) => {
							if (res.statusCode == 200) {
								console.log('uploadImage success, res is:', res)
								uni.hideLoading();
								uni.showToast({
									title: '上传成功',
									icon: 'success',
									duration: 1000
								})
								this.upLoadStatus = '上传成功'
								this.form.head = JSON.parse(res.data).data.url
								console.log(this.form.head)
							}



						},
						fail: (err) => {
							if (this.pageVisible) {
								console.log('uploadImage fail', err);
								uni.hideLoading();
								uni.showModal({
									content: err.errMsg,
									showCancel: false
								});
							}
						},
					});
				} else {
					const ts = new Date().getTime();
					// const ua = navigator.userAgent;
					const us = {
						ts,
						// ua
					};
					let url =this.$api.alyOssSignature+`${object2Url(us)}`;
					const res = await this.$http.get({
						url: url,
						data: {
							us: us
						}
					});

					let markIndex = this.avatarObj.path.lastIndexOf('.');
					let suffix = this.avatarObj.path.substring(markIndex);
					let date = new Date().getTime();
					wx.uploadFile({
						url: res.data.host, //上传的路径
						filePath: this.avatarObj.path,
						name: 'file',
						formData: {
							name: this.avatarObj.path,
							key: date + suffix, //上传图片的名字和路径（默认路径根目录，自定义目录：xxx/xxx.png）
							policy: res.data.policy,
							OSSAccessKeyId: res.data.accessId,
							success_action_status: "200",
							signature: res.data.signature,
						},
						header: {
							'Access-Control-Allow-Origin': '*',
							'x-oss-callback-Authorization': 'access_token'
						},
						success: (result)=> {
							console.log('chooseImage success, temp path is: ', res)

							let url = res.data.host + '/' + date + suffix;
							console.log(url) 
							this.form.head=url
							wx.showToast({
								title: "上传成功",
								icon: 'success',
								duration: 1000
							})
							
						},
						fail: function({
							errMsg
						}) {
							console.log('upladImage fail, errMsg is: ', errMsg)
							wx.showToast({
								title: "上传失败",
								duration: 1000
							})
						},
					})

				}
			},
			clk(index) {
				this.$refs.avatar.fChooseImg(index, {
					selWidth: "300upx",
					selHeight: "300upx",
					expWidth: '260upx',
					expHeight: '260upx'
				});
			},
			async clickAvaterFn() {
				if (!this.form.head) {
					this.$sun.toast('请上传头像')
					return
				}
<<<<<<< HEAD
				console.log(this.imageSrc)
				  this.uploadProgress({status:true,name:'uploadQualification'})
			}
=======
				console.log('请上传头像=================')
>>>>>>> 52b948355e151c415e9c82e94b1b31e76c5ad35e
			
				if(this.technicianDetails.authenticationNot == 3){
					console.log(this.technicianDetails)
					this.formData = this.technicianDetails
					const allForm={...this.formData,...this.form}
					console.log(JSON.stringify(allForm) +'========================')
					const saveRes = await this.$http.put({
						url: this.$api.technicianModifyIphone,
						data: {
							...allForm
						}
					});
					this.technicianDetail = allForm
					console.log(saveRes.data+'======================')
					this.technicianDetailsFn(this.technicianDetail ?this.technicianDetail:{})
					
					this.$sun.navBack();
				}else{
					this.uploadProgress({status:true,name:'uploadQualification'})
					console.log(this.form.head)
					this.uploadAvatar(this.form.head)
					// 退出这个页面
					this.$sun.navBack();
				}
				
				
			}

		}

	}
</script>

<style lang="less" scoped>
	.uploadAvatar {
		position: relative;
		text-align: center;
		padding: 40rpx;

		.circle {
			position: absolute;
			width: 472rpx;
			height: 472rpx;
			top: 50%;
			left: 50%;
			border-radius: 422rpx;
			transform: translate(-50%, -47%);
			border: 4rpx solid #2ED775;
		}

		.uploadImg {

			margin-top: 236rpx;
			width: 422rpx;
			height: 422rpx;
			border-radius: 422rpx;
		}

		.uploadSuccess {
			color: #2ED775;

			font-family: Inter;
			font-size: 16px;
			font-style: normal;
			font-weight: 400;


		}
	}

	.gouImg {
		width: 43rpx;
		height: 43rpx;
		margin-right: 5px;
	}

	.sumbit-btn {
		width: 670rpx;
		position: fixed;

		bottom: 50rpx;

		height: 100rpx;
		color: #fff;
		border-radius: 10px;
		background-color: #1890FF;
	}
</style>